package com.eastday.dao.impl;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.ArrayList;
import org.jdom.JDOMException;

import com.eastday.dao.impl.ConnectionBuilderDao;
import com.eastday.dao.ExportNewsDao;
import com.eastday.model.NewsInfoModel;

/**
 * 
 * @author dingxs
 *
 */
public class ExportNewsOracleDaoImpl extends ConnectionBuilderDao implements ExportNewsDao {

	public ExportNewsOracleDaoImpl()
	{
		super();
		try {
			this.conntoOracleDb();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		}
	}

	@Override
	public List<NewsInfoModel> ExportNews(int pagenum,String url) {
		// TODO Auto-generated method stub
		String sqlStr = "";
		
		PreparedStatement psForSelect = null;
		try {
			if(!url.trim().equals(""))
			{
				sqlStr = "SELECT channel,topic,link,timeinfo FROM t_news_import WHERE pageno=? AND pagesource=? ORDER BY timeinfo DESC";
				psForSelect = conn.prepareStatement(sqlStr);
				psForSelect.setInt(1, pagenum);
				psForSelect.setString(2, url);
			}
			else
			{
				sqlStr = "SELECT channel,topic,link,timeinfo FROM t_news_import WHERE pageno=? ORDER BY timeinfo DESC";
				psForSelect = conn.prepareStatement(sqlStr);
				psForSelect.setInt(1, pagenum);
			}
			ResultSet rs = psForSelect.executeQuery();
			List<NewsInfoModel> newsInfoModelList = new ArrayList<NewsInfoModel>();
			while(rs.next())
			{
				NewsInfoModel newsInfoModel = new NewsInfoModel();
				newsInfoModel.setChannel(rs.getString("channel"));
				newsInfoModel.setDatetime(rs.getString("timeinfo"));
				newsInfoModel.setLink(rs.getString("link"));
				newsInfoModel.setTopic(rs.getString("topic"));
				newsInfoModelList.add(newsInfoModel);
			}
			return newsInfoModelList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		} finally {
			try {
				psForSelect.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				this.exceptionHandler.writeToLog(e.getMessage());
			}
			this.closeConnection();
		}
		return null;
	}
	
	//得到该链接的最大页数
	public int selectMaxPage(String url){
		String sqlStr = "";
		if(!url.trim().equals(""))
		{
			sqlStr = "SELECT MAX(pageno) FROM t_news_import WHERE pagesource='"+url+"'";
		}
		else
		{
			sqlStr = "SELECT MAX(pageno) FROM t_news_import";
		}
		int maxCount = 0;
		try {
			PreparedStatement psForSelect = conn.prepareStatement(sqlStr);
			ResultSet rs = psForSelect.executeQuery();
			rs.next();
			maxCount = rs.getInt(1);
			return maxCount;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		}
		return 0;
	}
	
	
	
	
	
}
